﻿Public Class OutlookBar
    Inherits Panel

    Private mintButtonHeight As Integer
    Private mintSelectedBand As Integer
    Private selectedBandHeight As Integer

    Public Property ButtonHeight As Integer
        Get
            Return mintButtonHeight
        End Get
        Set(value As Integer)
            mintButtonHeight = value
        End Set
    End Property

    Public Property SelectedBand As Integer
        Get
            Return mintSelectedBand
        End Get
        Set(value As Integer)
            SelectBand(value)
        End Set
    End Property

    Public Sub Initialize()
        ' parent must exist 
        AddHandler Parent.SizeChanged, AddressOf SizeChangedEvent
    End Sub

    Public Sub AddBand(ByVal caption As String, ByVal content As ContentPanel)
        content.outlookBar = Me
        Dim index As Integer = Controls.Count
        Dim bti As BandTagInfo = New BandTagInfo(Me, index)
        Dim bandPanel As BandPanel = New BandPanel(caption, content, bti)
        Controls.Add(bandPanel)
        UpdateBarInfo()
        RecalcLayout(bandPanel, index)
    End Sub

    Public Sub SelectBand(ByVal index As Integer)
        mintSelectedBand = index
        RedrawBands()
    End Sub

    Private Sub RedrawBands()
        For i As Integer = 0 To Controls.Count - 1
            Dim bp As BandPanel = Controls(i)
            RecalcLayout(bp, i)
        Next
    End Sub

    Private Sub UpdateBarInfo()
        selectedBandHeight = ClientRectangle.Height - Controls.Count * ButtonHeight
    End Sub

    Private Sub RecalcLayout(ByVal bandPanel As BandPanel, ByVal index As Integer)
        Dim vPos As Integer = IIf(index <= SelectedBand, ButtonHeight * index, ButtonHeight * index + selectedBandHeight)
        Dim height = IIf(SelectedBand = index, selectedBandHeight + ButtonHeight, ButtonHeight)

        'the band dimensions
        bandPanel.Location = New Point(0, vPos)
        bandPanel.Size = New Size(ClientRectangle.Width, height)

        'the contained button dimensions
        bandPanel.Controls(0).Location = New Point(0, 0)
        bandPanel.Controls(0).Size = New Size(ClientRectangle.Width, ButtonHeight)

        'the contained content panel dimensions
        bandPanel.Controls(1).Location = New Point(0, ButtonHeight)
        bandPanel.Controls(1).Size = New Size(ClientRectangle.Width - 2, height - 8)
    End Sub

    Public Sub New()
        ButtonHeight = 25
        mintSelectedBand = 0
        selectedBandHeight = 0
    End Sub

    Private Sub SizeChangedEvent(ByVal sender As Object, ByVal e As EventArgs)
        Size = New Size(Size.Width, CType(sender, Control).ClientRectangle.Size.Height)
        UpdateBarInfo()
        RedrawBands()
    End Sub
End Class
